import React, {Component} from 'react';
import {
  ScrollView,
  Text,
  View,
  Image,
  StyleSheet,
  TouchableOpacity,
  Alert,
  WebView,
  ListView,
} from 'react-native';

import {
  Button,
  ButtonGroup,
} from 'react-native-elements';

import Platform from 'Platform';
import Config from '../../config';
import TImage from '../../components/TImage';
import Constants from '../../common/Constants';
import SizeUtils from '../../common/SizeUtils';
import ToastUtils from '../../common/ToastUtils';
import {DateUtils, HtmlUtils} from '../../utils/Utils';
import CommonStyles from '../../common/CommonStyles';
import ImageConstants from '../../common/ImageConstants';
import ActivityConstants from './ActivityConstants';
import BasePage from '../common/BasePage';
import BaseItem from '../common/BaseItem';
import Dimensions from 'Dimensions';
import CommonAvatarView from '../common/CommonAvatarView';
import CommonCheckUserTypeAction from '../../actions/common/CommonCheckUserTypeAction';
import ActivityRequestFollowAction from '../../actions/activity/ActivityRequestFollowAction';
import ActivityRequestEnterAction from '../../actions/activity/ActivityRequestEnterAction';
import ActivityRequestApplicantsAction from '../../actions/activity/ActivityRequestApplicantsAction';
import ActivityRequestContentAction from '../../actions/activity/ActivityRequestContentAction';
import ActivityRequestEntryListAction from '../../actions/activity/ActivityRequestEntryListAction';
import ActivityRequestEntryRule from '../../actions/activity/ActivityRequestEntryRule';
import CommonRequestUrlAction from '../../actions/common/CommonRequestUrlAction';
import CommonShowImageFullScreenAction from '../../actions/common/CommonShowImageFullScreenAction';
const BTN_GROUP_LABELS = ["活动详情", "报名详情", "选手热榜"];
const ITEM_HEIGHT = 170;

var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
export default class ActivityContentPage extends BasePage {
  constructor(props) {
    super(props);

    this._initState();

    this._firstUpdate = true;
    this.state.viewState = 1;
    this.state.statusText = "";
    this.state.urlData = "";
    this._saveContentPre = "";
    this.state.urlData = "";
    this._saveContentAfter = "";
    this.state.height = 400;
    this.isSucceed =  false;
  }

  componentDidMount() {
    this._requestData();
    this.webview =null;
  }

  componentWillUpdate(nextProps, nextState) {
    if(nextState.statusText != this.state.statusText && nextState.statusText != "") {
      ToastUtils.show(nextState.statusText);
      nextState.statusText = "";
    }
    if(nextState.isSucceed == true){
      if(nextState.data.activityData.content && nextState.data.activityData.content.indexOf("http") == 0 && nextState.urlData == "")
      this._requestContent(nextState.data.activityData.content);
      nextState.isSucceed  = false;
    }

    if(nextState.enter_state == 2) {
      nextState.enter_state = 0;
      this._requestData();
    }
  }
  _renderNavigationBarRightButtonContent() {
    return (
      <View style={{height:SizeUtils.translateDesignHeightToCode(51),
          flexDirection:"row", alignItems:"center", paddingTop:SizeUtils.translateDesignHeightToCode(0)}}>
        <TImage source={require("../../../resource/images/common/share.png")}
          style={{width:44, height:44}}/>
      </View>
    )
  }
  _onPressNavigationBarRightBtn() {
    this.setState({showShare:true});
  }
  _getShareData() {
    return {type:Constants.MODULE_TYPE.ACTIVITY,activity_id:this.state.data.activityData.activity_id,
      title:this.state.data.activityData.title}
  }
  render() {
    var height = SizeUtils.getWindowSize().height - Config.paddingTop - SizeUtils.translateDesignHeightToCode(100);
    return(
      <View style={{paddingTop:Config.paddingTop + SizeUtils.translateDesignHeightToCode(0),
          backgroundColor:"#ffffff", height:SizeUtils.getWindowSize().height}}>
        <View style={{height:height}}>
          <ScrollView style={{height:height}}>
            {this._renderContent()}
          </ScrollView>
        </View>
        {this._renderBottomBar()}
        {this._renderNavigationBar()}
        {this._renderShareView()}
      </View>
    )
  }

  _renderContent() {
    return(
      <View style={{flexDirection: "column"}}>
        <View style={{marginLeft:SizeUtils.translateDesignWidthToCode(0),
            marginRight:SizeUtils.translateDesignWidthToCode(0),
            }}>
          <TImage resizeMode={"cover"} source={ this._getShowImageSource( this.state.data.activityData ) }
              style={{ width:750, height:360,}} miniSize={"mini300_q80"} isMini={true}/>
          <View style={{
              flexDirection:'row',
              borderBottomWidth:SizeUtils.translateDesignNumToCode(3),
              height:SizeUtils.translateDesignHeightToCode(80),
              borderBottomColor:'#eeeeee',
              alignItems: 'center',
            }}>
            <Text style={{flex:1,fontSize:SizeUtils.translateFontSizeToCode(30), color:"#333333",
              marginLeft:SizeUtils.translateDesignNumToCode(40),
              marginRight:SizeUtils.translateDesignNumToCode(160),
              lineHeight:18,
              fontWeight:"bold"}}>
            {this.state.data.activityData.title}
          </Text>
          <View style={{alignItems:"center",
                  justifyContent: 'center',
                  width:44,
                  borderRadius:20,
                  backgroundColor:'#db3132',
                  position:'absolute',
                  right:10,
                  height:18,
                  top:SizeUtils.translateDesignHeightToCode(20)}}>
                <Text
                  style={[CommonStyles.secondaryText, {fontSize:SizeUtils.translateFontSizeToCode(18),
                    backgroundColor:'#0000',
                    color:'#fff',paddingLeft:5,paddingRight:5,paddingTop:2,paddingBottom:2}]}>
                    {this.props.pageData.routeData.manner===1?'团体赛':'个人赛'}
                </Text>
          </View>
          </View>
          <View style={{borderBottomColor:"#eeeeee",marginLeft:SizeUtils.translateDesignNumToCode(40),borderBottomWidth:SizeUtils.translateDesignNumToCode(3),paddingBottom:SizeUtils.translateDesignWidthToCode(26),}}>
            {
              this.state.data.activityData.apply_begin_at?
              <View style={{flexDirection:"row", alignItems:"center",marginTop:SizeUtils.translateDesignNumToCode(20),marginBottom:SizeUtils.translateDesignNumToCode(20)}}>
              <TImage source={require("../../../resource/images/common/shizhong.png")} style={{width:20,
                height:20, marginTop:SizeUtils.translateDesignNumToCode(30)}}/>
              <Text style={{fontSize:SizeUtils.translateFontSizeToCode(24), color:"#888888",
                marginTop:SizeUtils.translateDesignHeightToCode(20), marginLeft:SizeUtils.translateDesignWidthToCode(10)}}>
                {'报名时间：'+DateUtils.formatActivity(this.state.data.activityData.apply_begin_at) + " 到 " +
                DateUtils.formatActivity(this.state.data.activityData.apply_end_at )}
              </Text>
            </View>:null 
            }
            <View style={{flexDirection:"row", alignItems:"center",marginTop:SizeUtils.translateDesignNumToCode(this.state.data.activityData.apply_begin_at?0:20),marginBottom:SizeUtils.translateDesignNumToCode(this.state.data.activityData.apply_begin_at?0:20)}}>
              {this.state.data.activityData.apply_begin_at?
                <View style={{width:12}}/>:<TImage source={require("../../../resource/images/common/shizhong.png")} style={{width:20,
                height:20, marginTop:SizeUtils.translateDesignNumToCode(30)}}/>
              }
              <Text style={{fontSize:SizeUtils.translateFontSizeToCode(24), color:"#888888",
                marginTop:SizeUtils.translateDesignHeightToCode(this.state.data.activityData.apply_begin_at?0:20), marginLeft:SizeUtils.translateDesignWidthToCode(10)}}>
                {'活动时间：'+DateUtils.formatActivity(this.state.data.activityData.begin_at) + " 到 " +
                DateUtils.formatActivity(this.state.data.activityData.end_at)}
              </Text>
            </View> 
          </View>  
        </View>
        {this._renderAddress()}
        {this._renderEntry()}
        {/* {this._renderMap()} */}
        <View style={{backgroundColor:'#f2f2f2',height:SizeUtils.translateDesignHeightToCode(20)}}/>
        <View style={{marginTop:SizeUtils.translateDesignHeightToCode(0)}}>
          {this._renderView()}
        </View>
      </View>
    )
  }

  _getShowImageSource(data) {
    if( data.image_url ) {
      return {uri: this.state.data.activityData.image_url};
    }
    else {
      return require("../../../resource/images/quanzi/_tmp/02.png");
    }
  }

  _renderAddress() {
    return (
      <TouchableOpacity style={{flexDirection:"row", alignItems:"center",
          height:SizeUtils.translateDesignHeightToCode(70),
          borderBottomColor:"#f2f2f2",marginLeft:SizeUtils.translateDesignNumToCode(40),borderBottomWidth:SizeUtils.translateDesignNumToCode(3),
          }} onPress={this._onPressAdress.bind(this)}>
        <TImage source={require("../../../resource/images/common/map.png")} style={{width:20, height:20}}/>
        <Text style={{color:"#888888", marginLeft:SizeUtils.translateDesignWidthToCode(14), fontSize:SizeUtils.translateFontSizeToCode(24)}}>
        {this.getAddressText(this.state.data.activityData.address)}</Text>
      </TouchableOpacity>
    )
  }
  _renderEntry() {
    return (
      <View style={{height:90,borderBottomWidth:SizeUtils.translateDesignNumToCode(3),borderBottomColor:"#f2f2f2",}}>
        <View style={{height:30,flexDirection:'row',alignItems: 'center',justifyContent: 'space-between',
          }}>
          <View style={{flexDirection:'row'}}>
            <TImage source={require("../../../resource/images/activity/persion.png")} style={{marginLeft:12,width:30,height:30}} resizeMode={'contain'}/>
            <Text style={{fontSize:12}}>{this.state.data.activityData&&
              this.state.data.activityData.applicants_num||0}</Text>
          </View>
          {this.state.entryData&&this.state.entryData.length>0?<TouchableOpacity onPress={()=>{this.gotoPage(Config.pages.activity_userlist,{activity_id:this.props.pageData.routeData.activity_id});}}>
            <Text style={{color:'blue',marginRight:5,fontSize:13}}>{'查看更多'}</Text>
          </TouchableOpacity>:null}
        </View>
        {
          this.state.entryData&&this.state.entryData.length>0?<ListView
          style={{flex:1,paddingRight:0,paddingLeft:0,}}
          removeClippedSubviews={false}
          enableEmptySections={true}
          showsHorizontalScrollIndicator={false}
          horizontal={true}
          dataSource={ds.cloneWithRows(this.state.entryData||[])}
          renderRow={this._renderRow.bind(this)}
          />:
          <View style={{flex:1,alignItems: 'center',justifyContent: 'center',}}>
            <Text style={{color:"#888888", marginLeft:SizeUtils.translateDesignWidthToCode(20), fontSize:SizeUtils.translateFontSizeToCode(24)}}>暂无人员报名</Text>
          </View>}
        
      </View>
    );
  }
   _renderRow(data, index) {
    return (
      <View style={{height:50,width:50,justifyContent: 'center',alignItems: 'center',}}>
        <CommonAvatarView avatarUri={this._getImageSource(data)} size={60}/>
          <Text numberOfLines={1}
            style={[CommonStyles.primaryText, {marginTop:SizeUtils.translateDesignHeightToCode(5),
            marginBottom:SizeUtils.translateDesignHeightToCode(5),
            textAlign:'center',
            color:'#000',fontSize:SizeUtils.translateFontSizeToCode(16),}]}>
            {data.by_name}
          </Text>
      </View>
    );
  }
  _getImageSource(data) {
    if(data.avatar_url) {
      return {uri:data.avatar_url};
    }
    else {
      return {uri:""};
    }
  }
  _onPressAdress() {
    this.gotoPage(Config.pages.activity_map, {activityData:this.state.data.activityData});
  }

  getAddressText(address){
    if(!address){
      return "";
    }
   var adressArr = address.split("#_#");
   if(adressArr.length == 3){
    return adressArr[0];
   }
   return this.state.data.activityData.address;
  }

  _renderBottomBar() {
    return (
      <View style={{width:SizeUtils.getWindowSize().width, position:"absolute", flexDirection:"row",
          height:SizeUtils.translateDesignHeightToCode(110),
          bottom:0, justifyContent:"center",
          alignItems: "center", borderTopWidth:1,borderTopColor:"#cccccc",
          backgroundColor:"#ffffff"}}>
          {/* {this._renderEnterButton()} */}
          {/* {this._renderWatchButton()} */}
          {this._renderCommonButton()}
      </View>
    )
  }

  _renderEnterButton() {
    <Button
      buttonStyle={{width:SizeUtils.translateDesignWidthToCode(330), marginTop:SizeUtils.translateDesignHeightToCode(5),
          height:SizeUtils.translateDesignWidthToCode(70),
          borderColor:this._checkEnterDisable() ? "#cccccc" : "#db3132",
          borderWidth:1,
          left:10}}
        backgroundColor="#ffffff"
        borderRadius={5}
        disabled = {this._checkEnterDisable()}
        onPress={this._onPressEnter.bind(this)}
        title={this._getEnterBtnTitle()}
        textStyle={{color:this._checkEnterDisable() ? "#666666" : "#db3132", fontWeight:"bold"}}
    />
  }

  _renderCommonButton() {
    var title = "敬请期待";
    var buttonDisable = false;
    var pressButtonFuc = undefined;
    if(this.state.data.activityData.time_state == 0) {
      title = "敬请期待";
      buttonDisable = true;
    }
    else if(this.state.data.activityData.time_state == 3) {
      if(this.state.data.is_applied === 1) {
        title = "取消报名";
      }
      else {
        title = "立即报名";
      }
      pressButtonFuc = this._onPressEnter.bind(this);
    }
    else if(this.state.data.activityData.time_state == 1) {
      //填了直播的地址
      if(this.state.data.activityData.live_url != undefined && this.state.data.activityData.live_url != "") {
        title = "观看直播";
        pressButtonFuc = this._onPressWatch.bind(this);
      }
      else {
        title = "进行中";
        buttonDisable = true;
      }
    }
    else if(this.state.data.activityData.time_state == 2) {
      if(this.state.data.activityData.playback_url != undefined && this.state.data.activityData.playback_url != "") {
        title = "观看回放";
        pressButtonFuc = this._onPressWatch.bind(this);
      }
      else {
        title = "已结束";
        buttonDisable = true;
      }
    }
    return (
      <Button
        buttonStyle={{width:SizeUtils.getWindowSize().width,
            height:SizeUtils.translateDesignHeightToCode(110),}}
          backgroundColor = {buttonDisable ? "#cccccc" : "#db3132"}
          disabled = {buttonDisable}
          onPress={pressButtonFuc}
          title={title}
          textStyle={{color:"#ffffff", fontWeight:"bold"}}
      />
    )
  }

  _renderWatchButton() {
    return (
      <Button
          backgroundColor="#db3132"
          buttonStyle={{width:SizeUtils.translateDesignWidthToCode(330), marginTop:SizeUtils.translateDesignHeightToCode(5),
            height:SizeUtils.translateDesignWidthToCode(70),
            borderColor:this._checkWatchDisable() ? "#cccccc" : "#db3132",
            borderWidth:1,
            left:SizeUtils.translateDesignWidthToCode(-20)}}
          borderRadius={5}
          disabled = {this._checkWatchDisable()}
          onPress={this._onPressWatch.bind(this)}
          title={this._getWatchTitle()}
          textStyle={{color:this._checkWatchDisable() ? "#666666" : "#ffffff", fontWeight:"bold"}}
      />
    )
  }

  _getWatchTitle() {
    if(this.state.data.activityData.time_state == 2) {
      return "观看回放";
    }
    return "观看直播";
  }

  _checkEnterDisable() {
    return this.state.data.activityData.time_state == 1 || this.state.data.activityData.time_state == 2
      || this.state.enter_state == 1;
  }

  _checkWatchDisable() {
    return this.state.data.activityData.time_state == 0;
  }

  _dealEnterPage() {
    this.state.data = {activityData:this.props.pageData.routeData, is_applied:0};
    this._requestData();
  }

  _dealLeavePage() {
    this._initState();
  }

  _initState() {
    this.webview =null;
    this._saveContentPre = "";
    this._saveContentAfter = "";

    this.isSucceed =  false;
    this.state = {
      follow_state: 0,
      enter_state: 0,
      data: {activityData:this.props.pageData.routeData, is_applied:0},
      btnGroupSelectedIndex:0,
      reload:false,
      applicantsData: {},
      isResponeUpdate: false,
      showShare:false,
      webViewHeight:SizeUtils.getWindowSize().width,
    }
    this.state.urlData = "";
  }

  _getShowImageSource(contentData) {
    if(contentData.image_url) {
      return {uri: contentData.image_url};
    }
    else {
      return ImageConstants.common_default_ad;
    }
  }

  _renderTopBar() {
    return (
      <View style={{position:"absolute", height:SizeUtils.translateDesignHeightToCode(50),
          width:SizeUtils.getWindowSize().width,
          paddingLeft:SizeUtils.translateDesignWidthToCode(40), marginTop:SizeUtils.translateDesignHeightToCode(25),
          justifyContent:"center", alignItems:"center"}} >
          <View style={{width:SizeUtils.translateDesignWidthToCode(670), flexDirection:"row", justifyContent:"flex-end",
              paddingRight:SizeUtils.translateDesignWidthToCode(40),}}>
            <View style={{backgroundColor:"#aaaaaa", paddingLeft:SizeUtils.translateDesignWidthToCode(10),
                paddingRight:SizeUtils.translateDesignWidthToCode(10),
                height:SizeUtils.translateDesignHeightToCode(45),
                borderRadius:SizeUtils.translateDesignNumToCode(7), borderColor:"#f2f2f2"}}>
              <Text style={[CommonStyles.noticeText, {marginTop:SizeUtils.translateDesignHeightToCode(5),
                  color:"#ffffff", fontSize:SizeUtils.translateFontSizeToCode(24)}]}>
                浏览{this.state.data.activityData.readers_num}次
              </Text>
            </View>
            <View style={{backgroundColor:"#aaaaaa", marginLeft:SizeUtils.translateDesignWidthToCode(20),
                paddingLeft:SizeUtils.translateDesignWidthToCode(10),
                paddingRight:SizeUtils.translateDesignWidthToCode(10),
                height:SizeUtils.translateDesignHeightToCode(45),
                borderRadius:SizeUtils.translateDesignNumToCode(7), borderColor:"#f2f2f2"}}>
              <Text style={[CommonStyles.noticeText, {marginTop:SizeUtils.translateDesignHeightToCode(5),
                  color:"#ffffff", fontSize:SizeUtils.translateFontSizeToCode(24)}]}>
                报名{this.state.data.activityData.applicants_num}人
              </Text>
            </View>
          </View>
      </View>
    )
  }

  //报名详情，活动详情视图
  _renderView() {
    var text = "";
    if(this.state.btnGroupSelectedIndex == 2) {
      return;
    }
    else if(this.state.btnGroupSelectedIndex == 0) {
      text = this.state.data.activityData.content;
    }
    else if(this.state.btnGroupSelectedIndex == 1) {
      text = this.state.data.activityData.apply_content;
      // text = "本次比赛由大庆油田有限责任公司工会举办，比赛不收取报名费用，不设置名额限制，只要符合大赛报名要求（见上方图片消息）即可点击下方报名按键进行报名。"
    }

    if(this.state.btnGroupSelectedIndex == 0){
        return (<WebView style={{width:SizeUtils.getWindowSize().width - SizeUtils.translateDesignWidthToCode(80),marginLeft:SizeUtils.translateDesignWidthToCode(40),
          height:this.state.height - SizeUtils.translateDesignHeightToCode(76 - 20)}}
          onNavigationStateChange={(title)=>{
            if(title.title != undefined) {
              this.setState({
                height:(parseInt(title.title)+80)
              })
            }

             if(title.url && title.url!="" && title.url.indexOf("about:blank")<0
            && title.url.indexOf("react-js-navigation://postMessage") < 0
            && title.url.indexOf("data:text/html") != 0){
              var myUrl = title.url;
              if(Platform.OS === "android" && (myUrl.indexOf(".doc") > 0 || myUrl.indexOf(".xls") > 0 || myUrl.indexOf(".ppt") > 0)){
                myUrl = "https://view.officeapps.live.com/op/view.aspx?src=" + myUrl;
              }

              this.gotoPage(Config.pages.post_common_web, {url:myUrl});
            }
          }}
        scrollEnabled={false}
        javaScriptEnabled={true}
        onMessage={this._onWebViewMessage.bind(this)}
        source={HtmlUtils.getHtmlWeb(this._getContent(this.state.data.activityData),
          SizeUtils.getWindowSize().width-SizeUtils.translateDesignWidthToCode(120))}
        />);
    }

    return (
      <View style={{paddingLeft:SizeUtils.translateDesignWidthToCode(40), paddingRight:SizeUtils.translateDesignWidthToCode(40),}}>
        <Text style={{fontSize:SizeUtils.translateFontSizeToCode(30),
          color:"#999999",
          lineHeight:SizeUtils.translateDesignHeightToCode(45),}}>
          {text}
        </Text>
      </View>
    );
  }

   _onWebViewMessage(event) {
    if(!event.nativeEvent.data){
      return;
    }
    let dataArr = event.nativeEvent.data.split("#1#");
    new CommonShowImageFullScreenAction().call(this, {source:dataArr[0], isNeedSave:true})
  }

   _getContent(postData){
    let content = "加载中...";
    let hasContent = false;
    if(postData.content && postData.content.indexOf("http") != 0) {
      if(this._saveContentPre === postData.content) {
        content = this._saveContentAfter;
      }
      else {
        content = HtmlUtils.contentFormatImg(postData.content);
      }
      hasContent = true;
    }
    else if(this.state.urlData != ""){
      if(this._saveContentPre === this.state.urlData) {
        content = this._saveContentAfter;
      }
      else {
        content = HtmlUtils.contentFormatImg(this.state.urlData);
      }
      hasContent = true;
    }
    this._saveContentPre = postData.content;
    this._saveContentAfter = content;

    if(hasContent) {
      content = `<div style="font-size:20px">` + content + `</div>`;
      // content = HtmlUtils.contentFontSizeScale(content, this.state.changeFontSize)
    }
    else {
      content = `<div style="font-size:14px;text-align='center'">` + content + `</div>`;
    }
    return content;
  }

  _renderSingerView() {

  }

  _renderTopBtnGroup() {
    if(this.state.data.activityData.tag == 1) {
      return this._renderTopBtnGroupNormal();
    }
    else {
      return this._renderTopBtnGroupSinger();
    }
  }

  _renderTopBtnGroupNormal() {
    return(
      <View style={{flex:1, flexDirection:"row", marginLeft:0,
          marginTop: SizeUtils.translateDesignHeightToCode(65),
          marginRight:0, alignItems:"center", justifyContent:"center"}}>
        {this._renderBtnText(BTN_GROUP_LABELS[0], 0)}
        <View style={{width:SizeUtils.translateDesignWidthToCode(220),}}></View>
        {this._renderBtnText(BTN_GROUP_LABELS[1], 1)}
      </View>
    )
  }

  _renderTopBtnGroupSinger() {
    return(
      <View style={{flex:1, flexDirection:"row", marginLeft:0,
          marginTop: SizeUtils.translateDesignHeightToCode(65),
          marginRight:0, alignItems:"center", justifyContent:"center"}}>
        {this._renderBtnText(BTN_GROUP_LABELS[0], 0)}
        <View style={{width:SizeUtils.translateDesignWidthToCode(104),}}></View>
        {this._renderBtnText(BTN_GROUP_LABELS[1], 1)}
        <View style={{width:SizeUtils.translateDesignWidthToCode(104),}}></View>
        {this._renderBtnText(BTN_GROUP_LABELS[2], 2)}
      </View>
    )
  }

  _renderBtnText(value, index) {

    return (
      <TouchableOpacity key={index} onPress={() => {this._onPressTypeBtn(index)}}
          style={[this._getViewStyle(index),
            {height:SizeUtils.translateDesignHeightToCode(45),
            width:SizeUtils.translateDesignWidthToCode(132),
            borderBottomWidth: this.state.btnGroupSelectedIndex == index ? SizeUtils.translateDesignNumToCode(3) : 0}]}>
        <Text style={[this._getTextStyle(index),
          {fontSize:SizeUtils.translateFontSizeToCode(32)}]}>
          {value}
        </Text>
      </TouchableOpacity>
    )
  }

  _getViewStyle(index) {
    if(this.state.btnGroupSelectedIndex == index) {
      return style.view_selected;
    }
    return style.view_unselected;
  }

  _getTextStyle(index) {
    if(this.state.btnGroupSelectedIndex == index) {
      return style.btn_selected;
    }
    return style.btn_unselected;
  }

  _onPressTypeBtn(index) {
    if(index === 2) {
        this.gotoPage(Config.pages.activity_singer_list, {activityData:this.state.data.activityData});
    }
    else {
      if(this.state.btnGroupSelectedIndex != index) {
        this.setState({btnGroupSelectedIndex: index});
      }
    }
  }

  _getEnterBtnTitle() {
    var title = "立即报名";

    //0未开始，1进行中，2已结束, 3报名中 = ['0', '1', '2', '3'],
    if (this.state.enter_state == 1) {
      title = "报名中";
    }
    else {
      if(this.state.data.activityData.time_state == 1) {
        title = "进行中";
      }
      else if(this.state.data.activityData.time_state == 2) {
        title = "已经结束";
      }
      else if(this.state.data.activityData.time_state == 3) {
        if(this.state.data.is_applied == 1) {
          title = "取消报名";
        }
        else {
          title = "立即报名";
        }
      }
      else {
        title = "敬请期待";
      }
    }
    return title;
  }

  _onPressEnter() {
    //0未开始，1进行中，2已结束, 3报名中 = ['0', '1', '2', '3'],
    if(this.state.data.activityData.time_state == 1) {

    }
    else if(this.state.data.activityData.time_state == 2) {

    }
    else if(this.state.data.activityData.time_state == 3) {
      if(this.state.data.is_applied == 1) {
        Alert.alert('提示', "是否取消报名", [
            {text:'确定',onPress:()=>{this._onCancelApplicant()}},
            {text:'取消',onPress:()=>{}}
          ])
      }
      else {
        new CommonCheckUserTypeAction().call(this, {needLogin:true, needVerify:this.state.data.activityData.real_name_verify == 1}, this._onCheckIsUserCallback.bind(this));
      }
    }
  }

  _onCancelApplicant() {
    new ActivityRequestEnterAction().call(this, {activityData:this.state.data.activityData, type:"cancel"});
  }

  _onApplicantSubmit() {
    const {userData} = this.props.pageData.routeData;
    new ActivityRequestEnterAction().call(this, {activityData:this.state.data.activityData,avatar_url:userData.avatar_url, type:"enter"});
  }

  _onCheckIsUserCallback(result) {
    if(result) {
        //好声音活动
      if(this.state.data.activityData.tag == ActivityConstants.TYPE.SINGER) {
        this.gotoPage(Config.pages.activity_entrySinger, this.state.data.activityData);
      }
      else {
        if(this.state.data.activityData.apply_fields && this.state.data.activityData.apply_fields.length >0){
          var obj  = {manner:this.props.pageData.routeData.manner,...this.state.data.activityData,};
           obj.apply_fields=obj.apply_fields.concat(this.state.team_arr||[]);
           this.gotoPage(Config.pages.activity_enter_auto, obj);
        }else if(this.state.team_arr&&this.state.team_arr.length >0){
          var obj  = {manner:this.props.pageData.routeData.manner,...this.state.data.activityData,};
           obj.apply_fields=obj.apply_fields.concat(this.state.team_arr);
           this.gotoPage(Config.pages.activity_enter_auto, obj);
        }else{
          Alert.alert('提示', "是否确认报名", [
            {text:'确定',onPress:()=>{this._onApplicantSubmit()}},
            {text:'取消',onPress:()=>{}}
          ])
        }

      }
    }
  }

  _onPressWatch() {
    if(this.state.data.activityData.time_state == 1) {
      if(this.state.data.activityData.live_url != undefined && this.state.data.activityData.live_url != "") {
        this.gotoPage(Config.pages.mudu_view, {url:this.state.data.activityData.live_url});
      }
      else {
        ToastUtils.show("当前活动没有直播");
      }
    }
    else if(this.state.data.activityData.time_state == 2) {
      if(this.state.data.activityData.playback_url != undefined && this.state.data.activityData.playback_url != "") {
        this.gotoPage(Config.pages.mudu_view, {url:this.state.data.activityData.playback_url});
      }
      else {
        ToastUtils.show("当前活动没有重播");
      }
    }
    else {
      ToastUtils.show("活动还没开始");
    }
  }

  _onPressFollow() {
    new ActivityRequestFollowAction().call(this, this.state.data.activityData);
  }

  _requestData() {
    const {routeData} = this.props.pageData;
    new ActivityRequestContentAction().call(this, this.props.pageData.routeData);
    new ActivityRequestEntryListAction().call(this,{activity_id:routeData.activity_id,page:1,page_size:10,sort_id:1});
    if (this.props.pageData.routeData.manner===1) {
      new ActivityRequestEntryRule().call(this,{activity_id:routeData.activity_id});
    }
  }

   _requestContent(content) {
    new CommonRequestUrlAction().call(this, {url:content})
  }
}

var style = StyleSheet.create({
  view_selected: {
    borderBottomColor: "#db3132",
  },

  view_unselected: {
  },

  btn_selected: {
    color: "#db3132",
  },

  btn_unselected: {
    color: "#999999",
  },
})
